import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/widgets/bottom_sheet/default_bottom_sheet.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:r_datetime_picker/r_datetime_picker.dart';

showDateTimeSelectBottomSheet(
    BuildContext context, String title, DateTime select,
    {bool withinFinancialYear = true}) async {
  DateTime currentSelect = select?.add(Duration(seconds: 1)) ??
      DateTime.tryParse(Config.currentFY.startDate).add(Duration(seconds: 1));
  return await showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) => DefaultBottomSheet(
            title: title,
            child: DateTimeSelectBottomSheet(
              select: currentSelect,
              startDate: withinFinancialYear
                  ? DateTime.tryParse(Config.currentFY?.startDate) ??
                      DateTime(1900, 1, 1)
                  : DateTime(1900, 1, 1),
              endDate: withinFinancialYear
                  ? DateTime.tryParse(Config.currentFY?.endDate) ??
                      DateTime(2050, 12, 31)
                  : DateTime(2050, 12, 31),
              dateTimeCall: (dataTime) {
                currentSelect = dataTime;
              },
              onDone: () {
                Navigator.of(context).pop(currentSelect);
              },
            ),
          ));
}

class DateTimeSelectBottomSheet extends StatelessWidget {
  final ValueChanged<DateTime> dateTimeCall;
  final VoidCallback onDone;
  final DateTime select;
  final DateTime startDate;
  final DateTime endDate;
  const DateTimeSelectBottomSheet(
      {Key key,
      this.dateTimeCall,
      this.onDone,
      this.select,
      this.startDate,
      this.endDate})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        RDateTimePicker(
          initialDate: select ?? DateTime.now(),
          onChangedDate: _onChangeDateTime,
          minDate: startDate,
          maxDate: endDate,
          textColor: aceDarkGrayColor50,
          selectTextStyle: TextStyle(
            color: aceBlueColor,
            fontSize: getSp(18),
            fontWeight: FontWeight.w700,
            height: 22 / 18,
          ),
          unSelectTextStyle: TextStyle(
            color: aceDarkGrayColor50,
            fontSize: getSp(18),
            fontWeight: FontWeight.w400,
            height: 22 / 18,
          ),
          foregroundWidget: Container(
            height: getDp(42),
            decoration: BoxDecoration(
                border: Border.all(
              color: aceGrayColor,
              width: getDp(0.5),
            )),
          ),
        ),
        NormalButton(
          title: S.current.done,
          onTap: onDone,
        ),
      ],
    );
  }

  void _onChangeDateTime(DateTime value) {
    dateTimeCall?.call(value);
  }
}
